package org.deschutter.scans;

import org.deschutter.constants.Config;
import org.deschutter.scans.news.Incomming;
import org.deschutter.scans.news.Launch;
import org.hibernate.Session;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Berten De Schutter
 * Date: 01-Jul-2009
 * Time: 11:27:21
 */
public class News extends Scan {
    private ArrayList<Incomming> incomming;
    private ArrayList<Launch> launch;


    public News(String ID, BigDecimal tick, String x, String y, String z, String scanpage, BigDecimal userID, Session session) {
        super(ID, Config.NEWS_SCAN, tick, x, y, z, scanpage, userID,session);
    }

    protected String parseScan() {
        incomming = new ArrayList<Incomming>();
        launch = new ArrayList<Launch>();

        Pattern compiledRegex;
        Matcher regexMatcher;


        // Get incomming
        compiledRegex = Pattern.compile("<td class=\"left\" valign=\"top\">Incoming</td><td valign=\"top\">(\\d+)</td><td class=\"left\" valign=\"top\">We have detected an open jumpgate from ([^<]+), located at (\\d+):(\\d+):(\\d+). The fleet will approach our system in tick (\\d+) and appears to have (\\d+) visible ships.</td>");
        regexMatcher = compiledRegex.matcher(getScanpage());

        while (regexMatcher.find()) {
            Incomming tst = new Incomming(regexMatcher.group(3), regexMatcher.group(4), regexMatcher.group(5), new BigDecimal(regexMatcher.group(7)), new Integer(regexMatcher.group(1)), new Integer(regexMatcher.group(6)));
            incomming.add(tst);
        }

        // Get Launches - Attacks

        compiledRegex = Pattern.compile("<td class=\"left\" valign=\"top\">Launch</td><td valign=\"top\">(\\d+)</td><td class=\"left\" valign=\"top\">The ([^,]+) fleet has been launched, heading for (\\d+):(\\d+):(\\d+), on a mission to Attack. Arrival tick: (\\d+)</td>");
        regexMatcher = compiledRegex.matcher(getScanpage());

        while (regexMatcher.find()) {
            launch.add(new Launch(regexMatcher.group(3), regexMatcher.group(4), regexMatcher.group(5), "Attack", new Integer(regexMatcher.group(1)), new Integer(regexMatcher.group(6))));
        }

        // Get Launches - Defence

        compiledRegex = Pattern.compile("<td class=\"left\" valign=\"top\">Launch</td><td valign=\"top\">(\\d+)</td><td class=\"left\" valign=\"top\">The ([^<]+) fleet has been launched, heading for (\\d+):(\\d+):(\\d+), on a mission to Defend. Arrival tick: (\\d+)</td>");
        regexMatcher = compiledRegex.matcher(getScanpage());

        while (regexMatcher.find()) {
            launch.add(new Launch(regexMatcher.group(3), regexMatcher.group(4), regexMatcher.group(5), "Defence", new Integer(regexMatcher.group(1)), new Integer(regexMatcher.group(6))));
        }
        String ret = "*******************************\nNews Scan Added: " + getID();

        for (Incomming incs : incomming) {
            ret += "\nIncomming: " + incs.getX() + ":" + incs.getY() + ":" + incs.getZ();
        }

        for (Launch launched : launch) {
            ret += "\nLaunch: " + launched.getMission() + " : " + launched.getX() + ":" + launched.getY() + ":" + launched.getZ();
        }
        return ret + "\n*******************************";
    }

    public BigDecimal getTypeID() {
        return SCAN_TYPE_NEWS;
    }

    public ArrayList<Incomming> getIncomming() {
        return incomming;
    }

    public void setIncomming(ArrayList<Incomming> incomming) {
        this.incomming = incomming;
    }

    public ArrayList<Launch> getLaunch() {
        return launch;
    }

    public void setLaunch(ArrayList<Launch> launch) {
        this.launch = launch;
    }
}
